﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Puzzler.GUI
{
    /// <summary>
    /// Interaktionslogik für RectangleInputWindow.xaml
    /// </summary>
    public partial class RectangleInputWindow : Window
    {
        public static Rect? ShowDialog(Rect? rect)
        {
            RectangleInputWindow win = new RectangleInputWindow();
            if (rect.HasValue)
            {
                win.txtX.Text = rect.Value.Left.ToString();
                win.txtY.Text = rect.Value.Top.ToString();
                win.txtWidth.Text = rect.Value.Width.ToString();
                win.txtHeight.Text = rect.Value.Height.ToString();
            }

            if (win.ShowDialog().GetValueOrDefault(false))
            {
                return new Rect(
                    Double.Parse(win.txtX.Text),
                    Double.Parse(win.txtY.Text),
                    Double.Parse(win.txtWidth.Text),
                    Double.Parse(win.txtHeight.Text));
            }

            return null;
        }

        private RectangleInputWindow()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void txt_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!IsLoaded)
                return;

            double d;

            btnOk.IsEnabled =
                Double.TryParse(txtX.Text, out d) &&
                Double.TryParse(txtY.Text, out d) &&
                Double.TryParse(txtWidth.Text, out d) &&
                Double.TryParse(txtHeight.Text, out d);
        }
    }
}
